package com.imooc.service.impl;

import com.imooc.enums.YesOrNo;
import com.imooc.mapper.UserAddressMapper;
import com.imooc.pojo.UserAddress;
import com.imooc.pojo.bo.AddressBO;
import com.imooc.service.AddressService;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @description: 收获地址接口实现
 * @author: Yz
 * @create: 2020-06-24 09:31
 **/
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private Sid sid;

    /**
     * 根据用户id查询用户的收货地址列表
     * @param userId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<UserAddress> queryAll(String userId) {
        UserAddress ua = new UserAddress();
        ua.setUserId(userId);
        return userAddressMapper.select(ua);
    }

    /**
     * 用户新增收货地址
     * @param addressBO
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addNewUserAddress(AddressBO addressBO) {
        Integer isDefault = 0;
        //1.判断当前用户是否存在地址，如果没有，则新增为‘默认地址’
        List<UserAddress> userAddressList = this.queryAll(addressBO.getUserId());
        if (CollectionUtils.isEmpty(userAddressList)){
            isDefault = 1;
        }
        String addressId = sid.nextShort();
        //2.保存地址到数据库
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(addressBO,userAddress);
        userAddress.setId(addressId);
        userAddress.setIsDefault(isDefault);
        userAddress.setCreatedTime(new Date());
        userAddress.setUpdatedTime(new Date());
        userAddressMapper.insert(userAddress);
    }

    /**
     * 用户修改地址
     * @param addressBO
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateUserAddress(AddressBO addressBO) {
        String addressId = addressBO.getAddressId();
        UserAddress updateAddress = new UserAddress();
        BeanUtils.copyProperties(addressBO,updateAddress);
        updateAddress.setId(addressId);
        updateAddress.setUpdatedTime(new Date());
        //selective空数据 不会覆盖
        userAddressMapper.updateByPrimaryKeySelective(updateAddress);
    }

    /**
     * 根据用户id和地址id删除对应的用户地址信息
     * @param userId
     * @param addressId
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteUserAddress(String userId, String addressId) {
        UserAddress deleteUserAddress = new UserAddress();
        deleteUserAddress.setUserId(userId);
        deleteUserAddress.setId(addressId);
        userAddressMapper.delete(deleteUserAddress);
    }


    /**
     * 修改收货地址为默认地址
     * @param userId
     * @param addressId
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateUserAddressToBeDefault(String userId, String addressId) {
        //1.查找默认地址，设置为不默认
        UserAddress queryAddress = new UserAddress();
        queryAddress.setUserId(userId);
        queryAddress.setIsDefault(YesOrNo.YES.type);
        UserAddress defaultAddress = userAddressMapper.selectOne(queryAddress);
        defaultAddress.setIsDefault(YesOrNo.NO.type);
        userAddressMapper.updateByPrimaryKeySelective(defaultAddress);

        //2.根据地址id修改为默认地址
        UserAddress newDefaultAddress = new UserAddress();
        newDefaultAddress.setUserId(userId);
        newDefaultAddress.setId(addressId);
        newDefaultAddress.setIsDefault(YesOrNo.YES.type);
        userAddressMapper.updateByPrimaryKeySelective(newDefaultAddress);
    }

    /**
     * 根据用户id和地址id，查询具体的用户地址对象信息
     * @param userId
     * @param addressId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public UserAddress queryUserAddress(String userId, String addressId) {
        UserAddress queryAddress = new UserAddress();
        queryAddress.setUserId(userId);
        queryAddress.setId(addressId);
        return userAddressMapper.selectOne(queryAddress);
    }
}
